﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
public partial class backmanage_API_A_Card : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;
        string returnStr = string.Empty;
        string editType = Request.Form["edit"];
        switch (editType)
        {
            case "add":
                {
                    returnStr = CardNameAdd();
                    break;
                }
            case "update":
                {
                    break;
                }
            case "list":
                {
                    returnStr = CardDeputyList();
                    break;
                }
            case "gamelist":
                {
                  
                    returnStr = GameList();
                    break;
                }
            case "deputylist":
                {
                    returnStr = GetDeputyList();
                    break;
                }
            case "create":
                {
                    returnStr = CreateNewCard();
                    break;
                }
            case "cardlist":
                {
                    returnStr = CardList();
                    break;
                }
            default: break;
        }

        Response.Write(returnStr);
    }
    protected string CardList()
    {
        StringBuilder returnStr=new StringBuilder();
        BLL.CardMaster bll = new BLL.CardMaster();
        if (!string.IsNullOrEmpty(Request.Form["i"]))
        {
            DataSet ds = bll.CardMasterList(Request.Form["i"]);
            if (ds.Tables.Count > 0)
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        returnStr.Append("<tr>");
                        returnStr.Append("<td>" + dr[0].ToString() + "</td>");
                        returnStr.Append("<td>" + dr[1].ToString() + "</td>");
                        returnStr.Append("<td>" + dr[2].ToString() + "</td>");
                        returnStr.Append("<td>" + dr[3].ToString() + "</td>");
                        returnStr.Append("<td>" + dr[4].ToString() + "</td>");
                        returnStr.Append("</tr>");
                    }
                }
            }
        }
        return returnStr.ToString();
    }
    protected string CreateNewCard()
    {
        int numBer = Convert.ToInt32(Request.Form["n"]);
        string deputyId = Request.Form["i"];
        string returnStr = string.Empty;

        MODEL.CardMaster model = new MODEL.CardMaster();
        BLL.CardMaster bll = new BLL.CardMaster();
        bool flag = true;
        if (string.IsNullOrEmpty(Request.Form["e"]))
        {
            model.CEXPIRETIME = DateTime.Now.AddYears(100);
        }
        else
        {
            model.CEXPIRETIME = Convert.ToDateTime(Request.Form["e"]);
        }
        for (int i = 0; i < numBer; i++)
        {
            if (flag)
            {
                model.CCARDDEPUTYID = deputyId;
                model.CCARDNUM = GerRandomTimeNumber(i);
                model.CCREATETIME = DateTime.Now;
                model.CISEXPIRE = "F";
                model.CISUSE = "F";
                model.CUSETIME = null;
                flag = bll.CardMasterAdd(model);
            }
            else
            {
                numBer++;
                flag = true;
            }
        }
        return flag.ToString();
    }
    protected string GerRandomTimeNumber(int num)
    {
        string dt = DateTime.Now.AddSeconds(num).ToString("yyyMMddhhmmssfff");
        dt = "@)!@" + dt;
        dt = dt + "!@@!";
        return EncrypterMD5.MD5.Md5(dt, 16).ToUpper();
    }
    protected string CardNameAdd()
    {
        string returnStr = string.Empty;
        MODEL.CardDeputy model = new MODEL.CardDeputy();
        BLL.CardDeputy bll = new BLL.CardDeputy();
        model.CACTIVATION = Request.Form["a"];
        model.CCARDNAME = Request.Form["n"];
        model.CCARDNAMEINFO = Request.Form["i"];
        model.CCARDNUM = 0;
        model.CCARDTYEP = Convert.ToInt32(Request.Form["t"]);
        model.CGAMEID = Request.Form["g"];
        model.CISUSE = Request.Form["u"];
        model.CMONEY = Convert.ToInt32(Request.Form["m"]);
        model.CSHOW = Request.Form["w"];
        model.CSTATE = Request.Form["s"];

        bool flag = bll.CardDeputyAdd(model);
        if (flag)
        {
            returnStr = "{\"msg\":\"true\"}";
        }
        else
        {
            returnStr = "{\"msg\":\"false\"}";
        }
        return returnStr;
    }
    protected string CardDeputyList()
    {
        StringBuilder returnStr = new StringBuilder();
        DataSet ds = new BLL.CardDeputy().CardDeputyList();
        if (ds.Tables.Count > 0)
        {
            if (ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    returnStr.Append("<tr id=\""+dr["Cid"].ToString()+"\">");
                    returnStr.Append("<td>" + GetGameName(dr["Cgameid"].ToString()) + "</td>");
                    returnStr.Append("<td>" + dr["Ccardname"].ToString() + "</td>");
                    returnStr.Append("<td>" + dr["Ccardnameinfo"].ToString() + "</td>");
                    returnStr.Append("<td>" + GetState(dr["Ccardtype"].ToString()) + "</td>");
                    returnStr.Append("<td>" + dr["Cactivation"].ToString() + "</td>");
                    returnStr.Append("<td>" + dr["Ccardnum"].ToString() + "</td>");
                    returnStr.Append("<td>" + dr["Cmoney"].ToString() + "</td>");
                    returnStr.Append("<td>" + GetState(dr["Cisuse"].ToString()) + "</td>");
                    returnStr.Append("<td>" + GetState(dr["Cstate"].ToString()) + "</td>");
                    returnStr.Append("<td>" + GetState(dr["Cshow"].ToString()) + "</td>");
                    returnStr.Append("<td><a href=\"#\">修改</a></td>");
                    returnStr.Append("</tr>");
                }
            }
        }


        return returnStr.ToString();
    }
    protected string GetState(string chr)
    {
        string returnStr = string.Empty;
        switch (chr)
        {
            case "T":
                {
                    returnStr = "可用";
                    break;
                }
            case "F":
                {
                    returnStr = "不可用";
                    break;
                }
            case "0":
                {
                    returnStr = "新手卡";
                    break;
                }
            case "1":
                {
                    returnStr = "活动卡";
                    break;
                }
            default: break;

        }
        return returnStr;
    }
    protected string GetGameName(string id)
    {
        string returnStr = string.Empty;
        BLL.GameMaster bll = new BLL.GameMaster();
        if (id.Equals("0"))
        {
            returnStr = "所有游戏";
        }
        else
        {
            returnStr = bll.GameMasterList(id).Tables[0].Rows[0]["Gname"].ToString();
        }
        return returnStr;
    }
    protected string GameList()
    {
        StringBuilder returnStr = new StringBuilder();
        DataSet ds = new BLL.GameMaster().GameMasterList();
        if (ds.Tables.Count > 0)
        {
            if (ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    returnStr.Append("<option value=\"" + dr["Gid"] + "\">" + dr["Gname"].ToString() + "</option>");
                }
            }
        }
        return returnStr.ToString();
    }
    protected string GetDeputyList()
    {
        StringBuilder returnStr = new StringBuilder();
        DataSet ds = new BLL.CardDeputy().CardDeputyList();
        if (ds.Tables.Count > 0)
        {
            if (ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    returnStr.Append("<option value=\"" + dr["Cid"].ToString() + "\">" + dr["Ccardname"].ToString() + "</option>");
                }
            }
        }
        return returnStr.ToString();
    }
}
